/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    $('.upload-img-attachment').ajaxUpload({
        url: '/v1/upload',
        name:'attachment',
        onSubmit:function (data){
            $('#text-logo').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            var info = json.body.info[0];
            if(json.status==200){
                $('.upload-img-attachment>img').attr('src','/pictures/' + info.savename);
                $('.input-contract-attachment').val(info.savename);
                $('#text-logo').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //---------------------------------------------------------------------------------

    sendAjax('/v1/manager/contract/templates.json', 'get', {}, function(json) {
        var data = json.body.data;
        $.each(data, function(n, v) {
            var option = '<option value="'+ v.id+'">'+ v.name+'</option>';
            $('.template-id').append(option);
        });
        sendAjax('/v1/manager/contract/detail.json', 'get', {id:id}, function(json){
            var data = json.body.data;
            $('.contract-sn').val(data.sn);
            if (data.template_id != 0) {
                $('.template-id').val(data.template_id);
                $('.template-id option').each(function(){
                    if ($(this).val() == data.template_id) {
                        $(this).attr('selected', 'selected');
                    }
                });
            }

            $('.contract-user-id').val(data.user_id);
            $('.contract-realname').text(data.realname);
            $('.apart-id').val(data.apart_id);
            $('.apart-name').text(data.apart_name);
            $('.start-end-time').val(data.start_at+' ~ '+data.end_at);
            $('.order-at').val(data.order_at);
            $('.contract-rent').val(data.rent);
            $('.contract-deposit').val(data.deposit);
            $('input[name="is-renewable"]').each(function(){
                if ($(this).val() == data.is_renewable) {
                    $(this).prop('checked', 'checked');
                }
            });
            $('.contract-remark').val(data.remark);
            $('.input-contract-attachment').val(data.attachment);
            if (data.attachment) {
                $('.image-contract-attachment').attr('src', '/pictures/'+data.attachment);
            }
        });
    });

    $('.btn-save').on('click', function(){
        var data = {
            id : $('.contract-id').val(),
            sn : $('.contract-sn').val(),
            template_id : $('.template-id').val(),
            user_id : $('.contract-user-id').val(),
            realname : $('.contract-realname').val(),
            apart_id : $('.apart-id').val(),
            apart_name : $('.apart-name').val(),
            start_end_time : $('.start-end-time').val(),
            order_at : $('.order-at').val(),
            rent : $('.contract-rent').val(),
            deposit : $('.contract-deposit').val(),
            is_renewable : 0,
            remark : $('.contract-remark').val(),
            attachment : $('.input-contract-attachment').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/update/contract', 'post', {data:data}, function(json){
                var div = '<div><input type="hidden" class="jump-url" value="/manager/contract.html">' + json.message + '</div>';
                $('#action-tip .modal-body .tip-content').html(div);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        if ($(this).find('.jump-url').val()) {
            location.href = $(this).find('.jump-url').val();
        }
        $(this).find('.tip-content').text('');
    });
});

function checkForm(data) {
    if (!data.sn) {
        $('#action-tip .modal-body .tip-content').text('请填写合同单号');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.template_id) {
        $('#action-tip .modal-body .tip-content').text('请选择合同模板');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.user_id) {
        $('#action-tip .modal-body .tip-content').text('请选择承租人');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.apart_id) {
        $('#action-tip .modal-body .tip-content').text('请选择房源');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.start_end_time) {
        $('#action-tip .modal-body .tip-content').text('请选择起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (data.start_end_time.split(' ~ ').length < 2) {
        $('#action-tip .modal-body .tip-content').text('请选择起止时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (new Date(data.start_end_time.split(' ~ ')[1]) < new Date()) {
        $('#action-tip .modal-body .tip-content').text('结束时间不能小于当前时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (new Date(data.start_end_time.split(' ~ ')[1]) < new Date(data.start_end_time.split(' ~ ')[0])) {
        $('#action-tip .modal-body .tip-content').text('开始时间不能大于结束时间');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.rent) {
        $('#action-tip .modal-body .tip-content').text('请填写租金');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.deposit) {
        $('#action-tip .modal-body .tip-content').text('请填写押金');
        $('#action-tip').modal('show');
        return false;
    }
    return true;
}